package com.coolgor.config.web;

import com.coolgor.axnut.interceptor.auth.LoginInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.*;

@Configuration
@ComponentScan(basePackages = {"com.coolgor"})
public class WebMvcConfiguration implements WebMvcConfigurer {

    //spring boot中设置默认首页，当输入域名后自动跳转到默认指定的网页
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
//      registry.addViewController("/").setViewName("forward:/index");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        WebMvcConfigurer.super.addViewControllers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册拦截器 拦截规则
        //多个拦截器时 以此添加 执行顺序按添加顺序
        HandlerInterceptor loginInterceptor = new LoginInterceptor();
        List<String> loginInterceptorIncludePathPatterns = new ArrayList<String>();
        loginInterceptorIncludePathPatterns.add("/**");
        List<String> loginInterceptorExcludePathPatterns = new ArrayList<String>();
        loginInterceptorExcludePathPatterns.add("/");
        loginInterceptorExcludePathPatterns.add("/index*");
        loginInterceptorExcludePathPatterns.add("/public/**");//必须两个*，不然下层文件夹内的路径不得排除
        loginInterceptorExcludePathPatterns.add("/login/**");
        loginInterceptorExcludePathPatterns.add("/authlogin/*");
        loginInterceptorExcludePathPatterns.add("/wxqyhlogin/*");
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns(loginInterceptorIncludePathPatterns)
                .excludePathPatterns(loginInterceptorExcludePathPatterns);
    }

}